package com.rc.saas.tenant.service.system;

import com.rc.saas.tenant.mapper.system.RcSettingMapper;
import com.rc.saas.tenant.model.system.RcSetting;
import com.rc.saas.tenant.model.system.RcSettingExample;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RcSettingServiceImpl implements RcSettingService{

    @Autowired
    private RcSettingMapper settingMapper;

    @Override
    public RcSettingMapper getMapper() {
        return settingMapper;
    }

    @Override
    public void saveSetting(RcSetting setting) {
        if (setting.getApplySettingId() == null) {
            settingMapper.insertSelective(setting);
        } else {
            setting.setEnterpriseApplyType(StringUtils.isNotBlank(setting.getEnterpriseApplyType())?setting.getEnterpriseApplyType():"0");
            setting.setEnterpriseMemberApplyType(StringUtils.isNotBlank(setting.getEnterpriseMemberApplyType())?setting.getEnterpriseMemberApplyType():"0");
            setting.setChannelApplyType(StringUtils.isNotBlank(setting.getChannelApplyType())?setting.getChannelApplyType():"0");
            setting.setOperatorApplyType(StringUtils.isNotBlank(setting.getOperatorApplyType())?setting.getOperatorApplyType():"0");
            setting.setAdvanceApplyType(StringUtils.isNotBlank(setting.getAdvanceApplyType())?setting.getAdvanceApplyType():"0");
            setting.setMemberRefundApplyType(StringUtils.isNotBlank(setting.getMemberRefundApplyType())?setting.getMemberRefundApplyType():"0");
            setting.setInvoiceApplyType(StringUtils.isNotBlank(setting.getInvoiceApplyType())?setting.getInvoiceApplyType():"0");
            setting.setCardApplyType(StringUtils.isNotBlank(setting.getCardApplyType())?setting.getCardApplyType():"0");
            settingMapper.updateByPrimaryKey(setting);
        }
    }

    @Override
    public RcSetting selectSetting() {
        RcSettingExample example = new RcSettingExample();
        return settingMapper.selectByExample(example).stream().findFirst().orElseGet(() -> null);
    }
}
